/*
 * @Author: your name
 * @Date: 2021-12-10 14:58:03
 * @LastEditTime: 2021-12-10 14:58:23
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \gantt-vue2\src\utils\common.js
 */

const getDateNum = (n) => {
    if (n >= 0 && n < 10) {
        return "0" + n;
    }
    return "" + n;
};

/**
 * 格式化时间
 * @param timestamp 时间戳
 * @param type 格式
 * @returns
 */
 export const formatTime = (
    timestamp,
    type="yyyy-MM-dd HH:mm:ss"
) => {
    if (!timestamp) {
        return "";
    }
    timestamp = Number(timestamp);
    const date = new Date(timestamp);

    let str = "";
    const year = getDateNum(date.getFullYear());
    const month = getDateNum(date.getMonth() + 1);
    const day = getDateNum(date.getDate());
    const hour = getDateNum(date.getHours());
    const minute = getDateNum(date.getMinutes());
    const second = getDateNum(date.getSeconds());

    str = type
        .replace("yyyy", year)
        .replace("MM", month)
        .replace("dd", day)
        .replace("HH", hour)
        .replace("mm", minute)
        .replace("ss", second);

    return str;
};